import { Message, MessageBox } from 'element-ui'

function formatJson(filterVal: any, jsonData: any) {
    return jsonData.map((v: any) => filterVal.map((j: any) => v[j]));
}

const exportExcel = (selectlist: string[], header: string[], headertype: string[]) => {//导出文件为Excel文件
    if (selectlist.length < 1) {
        return Message.error('请选择您要导出的内容')
    }
    MessageBox.confirm("是否导出数据到Excel？", "确认信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确认导出",
        cancelButtonText: "放弃导出"
    }).then(() => {
        require.ensure([], () => {
            // eslint-disable-next-line @typescript-eslint/no-var-requires
            const { exportJsonToExcel } = require("../vendor/Export2Excel.js")
            const tHeader = header
            const filterVal = headertype
            const list = selectlist;
            const data = formatJson(filterVal, list);
            exportJsonToExcel(tHeader, data, "查询结果列表");
        })
    }).catch(() => {
        Message.success('已取消')
    })
}

const excelexport = (selectlist: string[], header: string[], headertype: string[]) => {
    exportExcel(selectlist, header, headertype)
}

export default excelexport